/*
 * RentalHandler.java
 *
 * Created on March 28, 2007, 8:42 PM
 */

package edu.byu.isys413.group1E;

import edu.byu.isys413.group1E.data.*;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Jon Tehero
 * Isys 413
 * Intex 2 - Mystuff
 */
public class RentalHandler_3 {
    private static RentalHandler_3 instance = null;
    
    /** Creates a new instance of RentalHandler */
    public RentalHandler_3() {
    }
    /**
     * Gets an new instance of a RentalHanlder
     *
     * @return instance of RentalHandler
     */
    public static synchronized RentalHandler_3 getInstance() {
        if (instance == null) {
            instance = new RentalHandler_3();
        }
        return instance;
    }
    
    /**
     * Returns an ArrayList<String> of all of the Rental Categories (Assuming that an item is available for rent)
     */
    public ArrayList<String> getRentalCategories(){
        ArrayList<String> categoryList = new ArrayList<String>();
        try {
            //retrieve the products
            ArrayList<ProductBO> prodList = (ArrayList<ProductBO>)ConceptualRentalDAO.getInstance().readAll();
            
            //convert them to conceptualRentalBO objects
            ArrayList<ConceptualRentalBO> crList = new ArrayList<ConceptualRentalBO>();
            for(int x=0;x<crList.size();x++){
                crList.add((ConceptualRentalBO)prodList.get(x));
            }
            
            //now parse them for the needed information
            categoryList = new ArrayList<String>();
            for (int i = 0; i < crList.size(); i++) {
                String temp = crList.get(i).getCategory().toString();
                if (categoryList.contains(temp)) {
                    //do nothing, this avoids adding duplicate categories
                } else {
                    categoryList.add(temp);
                }
            }
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        }
        return categoryList;
    }
    
    /**
     *creates a transaction with a blank sale
     *
     * @throws java.lang.Exception
     * @return
     */
    public TransLineBO createTXLineWithRental()throws Exception{
        TransLineDAO TXLineDAO = TransLineDAO.getInstance();
        TransLineBO TXLine = TXLineDAO.create();
        RSDAO_Factory factory = RSDAO_Factory.getInstance();
        RevenueSourceDAO revSrcDAO = factory.getDAO("rental");
        RevenueSourceBO revSrcBO = revSrcDAO.create();
        TXLine.setRevSrc(revSrcBO);
        return TXLine;
    }//end addRentalToTXLine
    /**
     *
     * @param saleBO
     * @param session
     * @throws edu.byu.isys413.group1E.DataException
     * @throws edu.byu.isys413.group1E.ConnectionPoolException
     * @throws edu.byu.isys413.group1E.SalesException
     */
    public void postRentalReservation(SaleBO saleBO, Session session) throws DataException, ConnectionPoolException, SalesException{
        ConceptualDAO conceptualDAO = ConceptualDAO.getInstance();
        ConceptualBO conceptualBO = null;
        ForSaleDAO forSaleDAO = ForSaleDAO.getInstance();
        ForSaleBO forSaleBO = null;
        StoreBO storeBO = session.getStore();
        EmployeeBO employeeBO = session.getEmployee();
        StoreProductDAO storeProductDAO = StoreProductDAO.getInstance();
        StoreProductBO storeProductBO = null;
        
        if(saleBO.getReturnLine()==true){
            
            if(saleBO.getProduct().getProdType().equals("physical")){
                forSaleBO = (ForSaleBO)saleBO.getProduct();
                if(forSaleBO.getNewUsed()==false){
                    throw new SalesException("You can't return a used Item.");
                }//end catch for returning a used item
                forSaleBO.setSold(false);
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), forSaleBO.getConceptual().getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()+saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
                forSaleDAO.save(forSaleBO);
            }//end posting of physical sale
            if(saleBO.getProduct().getProdType().equals("conceptual")){
                conceptualBO = (ConceptualBO)saleBO.getProduct();
                
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), conceptualBO.getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()+saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
            }//end posting of conceptual sale
            
        }if(saleBO.getReturnLine()==false){
            if(saleBO.getProduct().getProdType().equals("physical")){
                forSaleBO = (ForSaleBO)saleBO.getProduct();
                
                forSaleBO.setSold(true);
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), forSaleBO.getConceptual().getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()-saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
                forSaleDAO.save(forSaleBO);
            }//end posting of physical sale
            if(saleBO.getProduct().getProdType().equals("conceptual")){
                conceptualBO = (ConceptualBO)saleBO.getProduct();
                
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), conceptualBO.getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()-saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
            }//end posting of conceptual sale
            
        }//end posting of sale
        
        
    }//end post sale
    /**
     *
     * @param saleBO
     * @param quantity
     */
    public void changeRentalQuantity(SaleBO saleBO, int quantity){
        saleBO.setQuantity(quantity);
    }//end changeSaleQuantity
}